package cn.ante.ymcc.mq.consumer;

import cn.ante.ymcc.dto.PayAliNotifyDto;
import cn.ante.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup="alipay-course-consumer",//消费者组
        topic="paynofity-topic",//topic
        selectorExpression="aliay-tags",//tags
        messageModel= MessageModel.BROADCASTING//广播模式
        //还有一个广播模式，一个消息可以被多个消费者消费
)
public class CourseAliPayConsumer implements RocketMQListener<MessageExt> {


    @Autowired
    private ICourseUserLearnService courseUserLearnService;

    /**
     * 消费者只能成功消费一次
     *  消息签收  怎么标记为已签收？？
     *   答：你不报错就是已签收
     * 这个方法只要不报错，就是自动签收
     *
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        PayAliNotifyDto dto = JSONObject.parseObject(message, PayAliNotifyDto.class);
        courseUserLearnService.addUserLearn(dto);
    }
}